// subpkg/order-list/order-list.js
import { getMovieOrderList, delOrder } from '../../api/order'
// 参考：https://www.npmjs.com/package/wxmp-qrcode
import QR from 'wxmp-qrcode'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    movieOrderList: [], // 电影列表数据
    total: 0, // 总条数
    canvasId: 'canvasId',
    show: false,
    ticketNO: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getMovieOrderListData()
  },
  async getMovieOrderListData() {
    const res = await getMovieOrderList()

    this.setData({
      movieOrderList: res.data.items
    })
  },
  action(e) {
    const status = e.target.dataset.status
    const orderid = e.target.dataset.orderid
    const ticketNO = e.target.dataset.ticketno

    if (status === 4) {
      // 删除订单
      wx.showModal({
        title: '提示',
        content: '确定删除该订单吗？',
        confirmColor: '#e54847',
        complete: async (res) => {
          if (res.cancel) {

          }
          if (res.confirm) {
            await delOrder(orderid)

            // 重新获取
            this.getMovieOrderListData()
          }
        }
      })
    } else if (status === 2) {
      this.setData({
        show: true,
        ticketNO
      }, () => {
        setTimeout(() => {
          // 生成二维码
          const canvasId = this.data.canvasId
          QR.draw(ticketNO+'', canvasId)
        }, 200);
      })
    }
  },
  onClickHide() {
    this.setData({
      show: false
    })
  }
})